﻿// <file>
//     <copyright see="prj:///doc/copyright.txt"/>
//     <license see="prj:///doc/license.txt"/>
//     <owner name="Oliver Rau" email="mail@olira.de"/>
//     <version>$Revision: 3 $</version>
//     <created>11.08.2006 20:17</created>
// </file>

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace ns2kml
{
	/// <summary>
	/// Description of FrmMain.
	/// </summary>
	public partial class FrmMain
	{
		public FrmMain()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
		}
		
		void BtnSelectSourceClick(object sender, System.EventArgs e)
		{
			using (OpenFileDialog ofd = new OpenFileDialog()) {
				ofd.Title = "Select the source file";
				ofd.Filter = "Netstumbler files (*.ns1)|*.ns1|Text files (*.txt)|*.txt|All Files (*.*)|*.*";
				ofd.CheckFileExists = true;
				
				if (ofd.ShowDialog() == DialogResult.OK) {
					txtSource.Text = ofd.FileName;
					if (txtTarget.Text == "")
						txtTarget.Text = Path.Combine(Path.GetDirectoryName(txtSource.Text), 
						                              Path.GetFileNameWithoutExtension(txtSource.Text) + ".kml");
				}
			}
		}
		
		void BtnSelectTargetClick(object sender, System.EventArgs e)
		{
			using (SaveFileDialog sfd = new SaveFileDialog()) {
				sfd.Title = "Select the target file";
				sfd.Filter = "Google Earth KML|*.kml|All Files (*.*)|*.*";
				sfd.CheckFileExists = false;
				
				if (sfd.ShowDialog() == DialogResult.OK)
					txtTarget.Text = sfd.FileName;
			}
		}
		
		void BtnGenerateClick(object sender, System.EventArgs e)
		{
			if (!File.Exists(txtSource.Text))
				throw new FileNotFoundException();
			
			if (File.Exists(txtTarget.Text)) {
				DialogResult msgRes = MessageBox.Show("The target file already exists.\nDo you want to replace it?",
				                                      "Target file exists", MessageBoxButtons.YesNo, 
				                                      MessageBoxIcon.Question);
				
				if (msgRes == DialogResult.No)
					return;
			}
			
			List<APInfo> apInfo;
			
			if (Path.GetExtension(txtSource.Text) == ".ns1")
				apInfo = NS1FileHelper.GetInfoFromFile(txtSource.Text);
			else
				apInfo = WiScanFileHelper.GetInfoFromFile(txtSource.Text);
			
			StreamWriter sw = new StreamWriter(txtTarget.Text);
			sw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
			sw.WriteLine("<kml xmlns=\"http://earth.google.com/kml/2.0\">");
			sw.WriteLine("<Folder><name>" + Path.GetFileNameWithoutExtension(txtSource.Text) + "</name><open>0</open>");
			sw.WriteLine("<Folder><name>Networks (open)</name><open>0</open>");
			
			foreach (APInfo info in apInfo) {
				if (!info.HasFlag(WifiFlag.Privacy)) {
					AppendPlacemark(sw, info);
				}
			}
			
			sw.WriteLine("</Folder>");
			sw.WriteLine("<Folder><name>Networks (encrypted)</name><open>0</open>");
			
			foreach (APInfo info in apInfo) {
				if (info.HasFlag(WifiFlag.Privacy)) {
					AppendPlacemark(sw, info);
				}
			}
			sw.WriteLine("</Folder></Folder></kml>");
			
			sw.Flush();
			sw.Close();
			
			MessageBox.Show("Done!");
		}
		
		void AppendPlacemark(StreamWriter sw, APInfo info)
		{
			sw.WriteLine("<Placemark>");
			sw.WriteLine("\t<description><![CDATA[<b>SSID:</b> " + info.SSID + 
			             "<br /><b>BSSID:</b> " + info.BSSID +
			             "<br /><b>Vendor:</b> " + OuiHelper.Instance.GetOui(info.BSSID.Substring(0, 8)) +
			             "<br /><b>Last Update:</b> " + info.LastSeen.ToString() + 
			             "<br /><b>[SNR Signal Noise]:</b> " + info.MaxSNR + " " + 
			             info.MaxSignal + " " + info.MaxNoise + "]]></description>");
			sw.WriteLine("\t<name>" + info.SSID + "</name>");
			sw.WriteLine("\t<Style><IconStyle><Icon>");
			
			if (info.HasFlag(WifiFlag.Privacy)) {
				sw.WriteLine("\t\t<href>root://icons/palette-3.png</href>");
				sw.WriteLine("\t\t<x>64</x>");
				sw.WriteLine("\t\t<y>96</y>");
				sw.WriteLine("\t\t<w>32</w>");
				sw.WriteLine("\t\t<h>32</h>");
			} else {
				sw.WriteLine("\t\t<href>root://icons/palette-4.png</href>");
				sw.WriteLine("\t\t<y>160</y>");
				sw.WriteLine("\t\t<w>32</w>");
				sw.WriteLine("\t\t<h>32</h>");
			}
			
			sw.WriteLine("\t</Icon></IconStyle></Style>");
			sw.WriteLine("\t<Point><coordinates>" + info.BestLong.ToString(System.Globalization.CultureInfo.InvariantCulture) + "," + info.BestLat.ToString(System.Globalization.CultureInfo.InvariantCulture) + ",0</coordinates></Point>");
			sw.WriteLine("</Placemark>");
		}
	}
}
